package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.enums.ProdTypeEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.BProdStageInfo;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpOrganization;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.EnumUtils;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘玉亮.LL
 * @since 2021-06-10
 */
@Slf4j
@Api(tags = "产品管理：产品分期参数管理")
@RestController
@RequestMapping("/admin/mcmp/bProdStageInfo")
public class BProdStageInfoController extends BaseController {
    @Autowired
    private IBProdStageInfoService bProdStageInfoService;

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Autowired
    private IBBaseProdInfoService bBaseProdInfoService;

    @Autowired
    private IUpmpOrganizationService organizationService;

    @Autowired
    private IBProdInfoService bProdInfoService;

    @Autowired
    private IUpmpPrivilegeCheckService authPrivilegeCheckService;
  
    

    //@RequiresPermissions("产品管理:标准产品管理:产品分期参数")
    @RequestMapping(value = "/list", method = RequestMethod.GET)

    public  Map<String, Object>  findByPage(QueryRequest request,BProdStageInfo info) {
        return getDataTable(this.bProdStageInfoService.findByPage(request,info));
    }


    //@RequiresPermissions("产品管理:标准产品管理:产品分期参数")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)

    public OperationResult editSave(@ModelAttribute("bProdStageInfo") BProdStageInfo entity, Model model) {
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
       this.bProdStageInfoService.saveOrUpdate(entity);
        result.put("id", entity.getId());
        //TODO 添加实际业务操作
        return OperationResult.buildSuccessResult("数据保存处理完成", result);
    }

    @RequestMapping(value = "/prodUpdate", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "主键", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "prodType", value = "产品类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "opt", value = "操作类型", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "操作类型", required = true, dataType = "String")
    })
    public OperationResult prodUpdate(@ModelAttribute("bProdStageInfo") BProdStageInfo entity, HttpServletRequest request)throws RedpException {
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        String instId = request.getParameter("instId");
        String prodType = request.getParameter("prodType");
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        UpmpOrganization org = organizationService.getOrgByUser(user,instId);

        Map opt = EnumUtils.EnumToMap(PageOptFlagEnum.class,"getType","getDesc");

        String authCode =bBaseProdInfoService.getOprMenuName()+ BusinessContainer.AUTH_PRIVILEGE_CODE_SPLIT+ (null==opt.get(entity.getOptFlag())?"":opt.get(entity.getOptFlag()).toString());
        if(ProdTypeEnum.BASE_PROD.getType().equals(prodType)) {
            //TODO 添加实际业务操作
            return bProdStageInfoService.saveData(bBaseProdInfoService.getOprMenuId(), bBaseProdInfoService.getOprMenuName(), entity, user, org
                    , authPrivilegeCheckService, request, bBaseProdInfoService.getWebNextUrl(),authCode);
        }else if(ProdTypeEnum.PROD.getType().equals(prodType)){
            //TODO 添加实际业务操作
            return bProdStageInfoService.saveData(bProdInfoService.getOprMenuId(), bProdInfoService.getOprMenuName(), entity, user, org
                    , authPrivilegeCheckService, request, bProdInfoService.getWebNextUrl(),authCode);
        }else {
            return OperationResult.buildFailureResult("不支持prodType参数["+prodType+"]对应的产品类型！");
        }
    }
    //@RequiresPermissions("产品管理:标准产品管理:产品分期参数")
    @DeleteMapping("/{ids}")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.bProdStageInfoService.getBaseMapper().deleteBatchIds(Arrays.asList(ids.split(StringPool.COMMA)));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

}
